package org.hxyjs.controller;

import org.hxyjs.dto.DTO;
import org.hxyjs.entity.News;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.NewsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 newsController
@RequestMapping("/newsController") //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /newsController
public class NewsController {
    @Resource
    NewsService newsService;

    @RequestMapping("/getAllNews")
    @ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<News> getAllNews() throws Exception {
        System.out.println("查询新闻");
        return newsService.getAllNewss();
    }

    @RequestMapping("/addNews")
    @ResponseBody
    public DTO<News> addNews(@RequestBody News news) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
        System.out.println("增加新闻成功");
        return newsService.addNews(news);
    }

    @RequestMapping("/deleteNewsByNeno/{neno}")
    @ResponseBody
    public DTO<News> deleteNewsByNeno(@PathVariable("neno") String neno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
        System.out.println(neno + "删除新闻成功");
        return newsService.deleteNewsByNeno(neno);
    }


    @RequestMapping("/getNewsByNeno/{neno}")
    @ResponseBody
    public DTO<News> getNewsByNeno(@PathVariable("neno") String neno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
        System.out.println(neno + "根据主键查询新闻成功");
        return newsService.getNewsByNeno(neno);
    }

    @RequestMapping("/updateNewsByNeno")
    @ResponseBody
    public DTO<News> updateNewsByNeno(@RequestBody News news) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
        System.out.println("修改新闻成功");
        return newsService.updateNewsByNeno(news);
    }
    @RequestMapping("/getNewsByPage")
    @ResponseBody
    public DTO<News> getNewsByPage(PageInfo pageInfo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
        System.out.println("分页查询新闻成功");
        return newsService.getNewsByPage(pageInfo);
    }

}
